package com.example.openthedoor;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.example.openthedoor.databinding.DoorFragmentBinding;

import org.json.JSONException;
import org.json.JSONObject;

public class DoorFragment extends Fragment {
    DoorFragmentBinding binding;
    private MyViewModel myViewModel;
    private DataTemplateSample mDataTemplateSample;
    private JSONObject property = new JSONObject();     //属性的json
    private JSONObject metadata = new JSONObject();    //属性的metadata，目前只包含各个属性对应的时间戳

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        myViewModel = new ViewModelProvider(requireActivity()).get(MyViewModel.class);
        binding = DataBindingUtil.inflate(inflater, R.layout.door_fragment, container, false);
        binding.setData(myViewModel);

        Log.e("userDetail", "userName: "+myViewModel.getUserName()+"\nuserID: "+ myViewModel.getUserID()+"\npassWord: "+ myViewModel.getPassWord());

        mDataTemplateSample = new DataTemplateSample(requireContext());
        try {
            property.put("ifOpenTheDoor", 1);    //开门
        } catch (JSONException e) {
            e.printStackTrace();
        }


        binding.buttonUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller = Navigation.findNavController(v);
                controller.navigate(R.id.action_doorFragment_to_updateFragment);
            }
        });

        binding.buttonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
                builder.setTitle("确定要注销账号吗？");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //调用数据库接口，注销用户
                        myViewModel.delete();
                        myViewModel.delay(300);
                        if (myViewModel.getSuccess()) {
                            //注销用户人脸库
//                        try {
//                            myViewModel.faceDelete();
//                        } catch (JSONException e) {
//                            e.printStackTrace();
//                        }
                            Toast.makeText(getContext(), "Success!", Toast.LENGTH_SHORT).show();
                            NavController controller = Navigation.findNavController(v);
                            controller.navigate(R.id.action_doorFragment_to_loginFragment);
                        } else {
                            Toast.makeText(getContext(), myViewModel.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        binding.buttonOpen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过mqtt协议，发布开门指令
                mDataTemplateSample.propertyReport(property, metadata);
            }
        });

        return binding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mDataTemplateSample.disconnect();
    }

    @Override
    public void onStart() {
        super.onStart();
        mDataTemplateSample.connect();
    }

    @Override
    public void onPause() {
        super.onPause();
        mDataTemplateSample.disconnect();
    }
}
